#include <iostream>
#include <vector>
#include <string>
using namespace std;

class Solution {
public:
    int monotoneIncreasingDigits(int n) {
        string s = to_string(n);
        int m = s.size();
        int i = 0;
        while (i + 1 < m && s[i] <= s[i + 1]) i++;
        if (m == i + 1) return n;
        while (i - 1 >= 0 && s[i] == s[i - 1]) i--;
        s[i]--;
        for (int j = i + 1; j < m; j++) s[j] = '9';
        return stoi(s);
    }
};

int main()
{
    Solution s;
    int n = 332;
    int ret = s.monotoneIncreasingDigits(n);
    cout << ret << endl;
    return 0;
}

class Solution {
public:
    int ret = 0;
    int end, begin;
    int brokenCalc(int startValue, int target) {
        end = target, begin = startValue;
        // end -> begin
        if (end <= begin)
        {
            ret = begin - end;
            return ret;
        }
        else // end > begin
        {
            while (end > begin)
            {
                if (func()) ret++;
                else ret += 2;
            }
            ret += begin - end;
            return ret;
        }
    }
    bool func()
    {
        if (end % 2 == 0)
        {
            end /= 2;
            return true;
        }
        else
        {
            end++;
            end /= 2;
            return false;
        }
    }
};

class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        vector<vector<int>> ret;
        sort(intervals.begin(), intervals.end());
        int left = intervals[0][0], right = intervals[0][1];
        for (int i = 1; i < intervals.size(); i++)
        {
            int a = intervals[i][0];
            int b = intervals[i][1];
            if (a <= right)
            {
                right = max(right, b);
            }
            else
            {
                ret.push_back({ left, right });
                left = a;
                right = b;
            }
        }
        ret.push_back({ left, right });
        return ret;
    }
};